using System;
using System.Collections.Generic;
using System.Linq;
using BuildBox.Core.Server;
using BuildBox.Core.Server.Requests;

namespace BuildBox.Core.BootstrapperTasks
{
    public class RegisterRequests : IBootstrapperTask
    {
        private readonly IList<Request> _requests;

        public RegisterRequests(IList<Request> requests)
        {
            _requests = requests;
        }

        public void Execute()
        {
            Add<RootRequestHandler>(@"^/$", HttpVerb.Get);
            Add<FaviconRequestHandler>(@"^favicon.ico$", HttpVerb.Get);

            // non-route based requests such as CSS, Images, JS, Etc..
            Add<ImageRequestHandler>(@"^Assets/Images", HttpVerb.Get);
            Add<FileRequestHandler>(@"^.+\.((htm)|(html)|(css)|(js))$", HttpVerb.Get, HttpVerb.Post);
        }

        protected void Add<T>(string urlPattern, params string[] verbs)
        {
            _requests.Add(new Request
                              {
                                  UrlPattern = urlPattern,
                                  Verbs = verbs,
                                  Type = typeof(T)
                              });            
        }
    }
}